package com.situ.library.dao.impl;

import com.situ.library.dao.ICardDao;
import com.situ.library.pojo.entity.Card;
import com.situ.library.pojo.query.CardQuery;
import com.situ.library.pojo.vo.CardVO;
import com.situ.library.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

public class CardDaoImpl implements ICardDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<CardVO> selectByPage(CardQuery cardQuery) {
        String sql = "SELECT c.id, c.`name`, c.photo, c.unit, c.address, c.phone, c.`status`, c.create_time,\n" +
                "       i.id AS identityId, i.`name` AS identityName\n" +
                "FROM card AS c\n" +
                "INNER JOIN identity AS i ON c.identity_id = i.id";
        //构造一个集合存放所有的条件
        List<Object> queryList = new ArrayList<>();
        String queryName = cardQuery.getName();
        Integer queryId = cardQuery.getId();
        if (queryName != null && !queryName.equals("")) {
            sql += " and c.`name` like ?";
            queryList.add("%" + queryName + "%");
        }
        if (queryId != null && !queryId.equals("")) {
            sql += " and c.id like ?";
            queryList.add("%" + queryId + "%");
        }
        if (!ObjectUtils.isEmpty(cardQuery.getBeginCreateTime()) &&
                !ObjectUtils.isEmpty(cardQuery.getEndCreateTime())) {
            sql += " and create_time between ? and ?";
            queryList.add(cardQuery.getBeginCreateTime());
            queryList.add(cardQuery.getEndCreateTime());
        }
        sql += " limit ?,?";
        int offset = (cardQuery.getPage() - 1) * cardQuery.getLimit();
        queryList.add(offset);
        queryList.add(cardQuery.getLimit());
        List<CardVO> list = template.query(sql,
                new BeanPropertyRowMapper<>(CardVO.class), queryList.toArray());
        return list;
    }

    @Override
    public int selectTotalCount(CardQuery cardQuery) {
        String sql = "select count(*) from card where 1=1";
        //构造一个集合存放所有的条件
        List<Object> queryList = new ArrayList<>();
        String queryName = cardQuery.getName();
        if (queryName != null && !queryName.equals("")) {
            sql += " and name like ?";
            queryList.add("%" + queryName + "%");
        }
//        Integer count = template.queryForObject(sql, Integer.class,queryList);
//        return count;
        return template.queryForObject(sql, Integer.class, queryList.toArray());
    }

    @Override
    public void deleteById(int id) {
        template.update("delete from card where id=?", id);
    }

    @Override
    public void add(Card card) {
        template.update("insert into card(name,photo,unit,address,phone,identity_id) values(?,?,?,?,?,?)",
                card.getName(), card.getPhoto(), card.getUnit(), card.getAddress(), card.getPhone(), card.getIdentityId());
    }

    @Override
    public Card selectById(int id) {
        return template.queryForObject("select * from card where id=?",
                new BeanPropertyRowMapper<>(Card.class), id);
    }

    @Override
    public void update(Card card) {
        template.update("update card set name=?, photo=?, unit=?,address=?,phone=?,identity_id=?  where id=?",
                card.getName(), card.getPhoto(), card.getUnit(), card.getAddress(), card.getPhone(), card.getIdentityId(), card.getId());
    }

    @Override
    public List<Card> selectAll() {
        return template.query("select * from card", new BeanPropertyRowMapper<>(Card.class));
    }

    @Override
    public void updateStatus(int id, int status) {
        template.update("update  card set status=? where id=?",
                status, id);
    }
}

